// pages/addresslist/addresslist.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addresslist: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getVillage();
  },
  getVillage(){
    var that = this;
    app.ajax({
      url: 'Partner/WaterCard/getPartnerVillage',
      data: {},
      load:true,
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            addresslist:res.data.data
          });
          
        }
      }
    }) 
    
  },
  


  defaultaddress: function (e) {
    console.log(e);
  },

  // 删除地址
  deladdress: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var addresslist = that.data.addresslist;
    addresslist.splice(index, 1);
    wx.showModal({
      title: '提示',
      content: '是否删除此地址',
      success: function (res) {
        if (res.confirm) {
          that.setData({
            addresslist: addresslist
          });
          wx.showToast({
            title: '删除成功',
            icon: 'none',
            duration: 2000
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  // 编辑地址
  editaddress:function(){
    wx.navigateTo({
      url: '../addaddress/addaddress',
    })
  },

  // 新增地址
  addaddress: function () {
    wx.navigateTo({
      url: '../addaddress/addaddress',
    })
  },


  choosethis:function(e){
    var id = e.currentTarget.dataset.id;
    app.ajax({
      url: 'Partner/WaterCard/setDefaultVillage',
      data: { vid: id},
      load: true,
      success: function (res) {
        if (res.data.code == 1000) {
          
        }
      }
    }) 
    var village_name = e.currentTarget.dataset.village;
    var province = e.currentTarget.dataset.province;
    var city = e.currentTarget.dataset.city;
    var area = e.currentTarget.dataset.area;
    var address = e.currentTarget.dataset.province_name + e.currentTarget.dataset.city_name + e.currentTarget.dataset.area_name + village_name;
    wx.navigateTo({
      url: '../fillin_info/fillin_info?v_id=' + id + '&village_name=' + village_name + '&province=' + province + '&city=' + city + '&area=' + area + '&address=' + address,
    })
  }

})